/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.project.dao.ServiceConfigDao;
import io.renren.modules.project.entity.FrontendEntity;
import io.renren.modules.project.entity.PublishEntity;
import io.renren.modules.project.entity.ServiceConfigEntity;
import io.renren.modules.project.service.FrontendService;
import io.renren.modules.project.service.ServiceConfigService;
import io.renren.modules.systemconfig.entity.ProjectBaseEntity;
import io.renren.modules.systemconfig.service.ProjectBaseService;
import io.renren.modules.systemconfig.service.SysConfigServerService;
import io.renren.ssh.SFTPClient;
import io.renren.ssh.SSHClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.convert.EntityWriter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("serviceConfigService")
public class ServiceConfigServiceImpl extends ServiceImpl<ServiceConfigDao, ServiceConfigEntity> implements ServiceConfigService {

	@Autowired
	private FrontendService frontendService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String type = (String)params.get("type");
		String objectId = (String)params.get("objectId");
		IPage<ServiceConfigEntity> page = this.page(
				new Query<ServiceConfigEntity>().getPage(params),
				new QueryWrapper<ServiceConfigEntity>()
						.eq(StringUtils.isNotBlank(type),"type", type).eq(StringUtils.isNotBlank(objectId),"object_id", objectId).orderByDesc("create_time")
		);

		return new PageUtils(page);
	}

	@Override
	@Transactional
	public void saveEntity(ServiceConfigEntity entity) {
		this.baseMapper.insert(entity);
	}

	@Override
	@Transactional
	public void updateNewVersion(ServiceConfigEntity entity) throws Exception{
				long frontId = entity.getObjectId();
				FrontendEntity fe=frontendService.getAllInfoById(frontId);
				if(fe!=null&&fe.getServer()!=null){
						SFTPClient sftp = new SFTPClient(fe.getServer());
						sftp.uploadContent(fe.getNginxConfigPath(),fe.getNginxConfigName(),entity.getContent());
						sftp.close();
				}


				ServiceConfigEntity oldse=new ServiceConfigEntity();
				oldse.setStatus(0);
				this.baseMapper.update(oldse,new UpdateWrapper<ServiceConfigEntity>()
						.eq(true,"type", entity.getType()).eq(true,"object_id", entity.getObjectId()));
				this.baseMapper.insert(entity);
	}


	@Override
	@Transactional
	public void update(ServiceConfigEntity user) {
		this.updateById(user);
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}


}